/*
    SUSAN® - Sole of Unix Save ANything

   Copyright (C) 2018-2020 Hechzh Property

   
*/
#ifndef LIB_CRAM_MD5_H_
#define LIB_CRAM_MD5_H_

#include "lib/tls_conf.h"

class BareosSocket;

class CramMd5Handshake {
 public:
  CramMd5Handshake(BareosSocket* bs,
                   const char* passwort,
                   TlsPolicy local_tls_policy,
                   const std::string& own_qualified_name);
  bool DoHandshake(bool initiated_by_remote);
  TlsPolicy RemoteTlsPolicy() const { return remote_tls_policy_; }
  std::string destination_qualified_name_;

  enum class HandshakeResult
  {
    NOT_INITIALIZED,
    SUCCESS,
    FORMAT_MISMATCH,
    NETWORK_ERROR,
    WRONG_HASH,
    REPLAY_ATTACK
  };

  mutable HandshakeResult result{HandshakeResult::NOT_INITIALIZED};

 private:
  static constexpr int debuglevel_ = 50;
  bool compatible_ = true;
  BareosSocket* bs_ = nullptr;
  const char* password_ = nullptr;
  TlsPolicy local_tls_policy_ = kBnetTlsUnknown;
  TlsPolicy remote_tls_policy_ = kBnetTlsUnknown;
  const std::string own_qualified_name_;
  std::string own_qualified_name_bashed_spaces_;
  bool CramMd5Challenge();
  bool CramMd5Response();
  void InitRandom() const;

  enum class ComparisonResult
  {
    FAILURE,
    IS_SAME,
    IS_DIFFERENT
  };

  ComparisonResult CompareChallengeWithOwnQualifiedName(
      const char* challenge) const;
};

void hmac_md5(uint8_t* text,
              int text_len,
              uint8_t* key,
              int key_len,
              uint8_t* hmac);

#endif  // LIB_CRAM_MD5_H_
